package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.dto.req.orderInst.*;
import com.vm.osm.business.dto.rsp.customer.Customer;
import com.vm.osm.business.dto.rsp.orderInst.RspOrderPage;
import com.vm.osm.business.service.IMyOrderInfoService;
import com.vm.osm.business.util.UserDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by chenzui on 2018/3/23.
 */
@RestController
@RequestMapping("/v1.0")
@Api(value = "MyFOrderController",description = "我的工单模块--故障单",tags = {"MyFOrderController"})
public class MyFailOrderOperatorController extends ControllerExceptionHandler{
    private Logger LOG = Logger.getLogger(this.getClass());
    @Autowired
    private IMyOrderInfoService myOrderInfoService;

    @ApiOperation(value = "获取我的工单列表",notes = "获取我的工单列表--指派给我的。我可以抢的")
    @ApiImplicitParam(name = "reqGetMyOrder",value = "请求参数",required = true,dataType = "ReqGetMyOrder" )
    @RequestMapping(value = "/my/orders",method = RequestMethod.POST)
    public RspOrderPage queryMyOrder(@RequestBody ReqGetMyOrder reqGetMyOrder,HttpServletRequest request){
        LOG.info("获取我的工单列表");
        reqGetMyOrder.setStaff_id(UserDataUtil.getStaffId(request));
        reqGetMyOrder.setEnterprise_id(UserDataUtil.getUserSelectedEnterpriseId(request));
        return myOrderInfoService.queryMyOrders(reqGetMyOrder);
    }
    @ApiOperation(value = "故障单接单",notes = "故障单接单")
    @RequestMapping(value = "/my/agree_fail",method = RequestMethod.PUT)
    public void agree(HttpServletRequest request,
          @ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId){
        LOG.info("故障单接单");
        myOrderInfoService.agree(orderId,UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),UserDataUtil.getStaffHeadImg(request));
    }
    @ApiOperation(value = "故障单拒绝接单",notes = "故障单拒绝接单")
    @RequestMapping(value = "/my/disagree_fail",method = RequestMethod.PUT)
    public void disAgree(@ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId,
                 @ApiParam(value = "type",name="type",required = true) @RequestParam("type") String type,
                 @ApiParam(value = "reason",name="reason",required = true) @RequestParam("reason") String reason,
                 HttpServletRequest request){
        LOG.info("故障单拒绝接单");
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String staffIcon = UserDataUtil.getStaffHeadImg(request);
        myOrderInfoService.disagree(orderId,type,reason,staffId,staffName,staffIcon);
    }

    @ApiOperation(value = "接单后填写工单信息",notes = "接单后填写工单信息")
    @ApiImplicitParam(name = "reqDealOrderFailure",value = "请求参数",required = true,dataType = "ReqDealOrderFailure" )
    @RequestMapping(value = "/my/update_order",method = RequestMethod.POST)
    public void updateOrderFailure(@RequestBody ReqDealOrderFailure reqDealOrderFailure,HttpServletRequest request){
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String headIcon = UserDataUtil.getStaffHeadImg(request);
        reqDealOrderFailure.setStaff_id(staffId);
        reqDealOrderFailure.setStaff_name(staffName);
        reqDealOrderFailure.setHead_icon(headIcon);
        myOrderInfoService.updateOrderFailure(reqDealOrderFailure);
    }

    @ApiOperation(value = "转单接口",notes = "转单接口")
    @ApiImplicitParam(name = "reqTurnOrder",value = "请求参数",required =true,dataType = "ReqTurnOrder")
    @RequestMapping(value = "/my/turn",method = RequestMethod.PUT)
    public void turnToOthers(@RequestBody ReqTurnOrder reqTurnOrder,HttpServletRequest request){
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String headIcon = UserDataUtil.getStaffHeadImg(request);
        reqTurnOrder.setStaff_id(staffId);
        reqTurnOrder.setStaff_name(staffName);
        reqTurnOrder.setHead_icon(headIcon);
        myOrderInfoService.turn(reqTurnOrder);
    }

    @ApiOperation(value = "查询我的服务商",notes = "查询我的服务商")
    @RequestMapping(value = "/my/providers",method = RequestMethod.POST)
    public List<Customer> queryServerProviders(HttpServletRequest request){
        LOG.info("查询我的服务商");
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        LOG.info("企业ID:"+enterpriseId);
        return myOrderInfoService.queryProviders(enterpriseId);
    }

    @ApiOperation(value = "故障上报",notes = "故障上报")
    @ApiImplicitParam(value = "reqUpOrder",name = "reqUpOrder",dataType = "ReqUpOrder",required = true)
    @RequestMapping(value = "/my/up_order",method = RequestMethod.POST)
    public void upOrder(@RequestBody ReqUpOrder reqUpOrder,HttpServletRequest request){
        LOG.info("故障上报");
        reqUpOrder.setHead_icon(UserDataUtil.getStaffHeadImg(request));
        reqUpOrder.setStaff_id(UserDataUtil.getStaffId(request));
        reqUpOrder.setStaff_name(UserDataUtil.getStaffName(request));
        myOrderInfoService.up(reqUpOrder);
    }

    @ApiOperation(value = "故障误报",notes = "故障误报")
    @ApiImplicitParam(value = "reqBackOrder",name = "reqBackOrder",dataType = "ReqBackOrder",required = true)
    @RequestMapping(value = "/my/back",method = RequestMethod.PUT)
    public void back(@RequestBody ReqBackOrder reqBackOrder){
        LOG.info("故障误报");
        myOrderInfoService.back(reqBackOrder);
    }

    @ApiOperation(value = "上报完，内外部分配公司或者员工",notes = "上报中的订单进行再次派单")
    @ApiImplicitParam(value = "reqAssignOrder",name = "reqAssignOrder",dataType = "ReqAssignOrder",required = true)
    public void dealUpOrder(@RequestBody ReqAssignOrder reqAssignOrder,HttpServletRequest request){
        LOG.info("上报中的订单进行再次派单");
        myOrderInfoService.dealUpOrder(reqAssignOrder,request);
    }

    @ApiOperation(value = "抢单",notes = "抢单")
    @RequestMapping(value = "/my/catch/{id}",method = RequestMethod.PUT)
    public void catchOrder(
            HttpServletRequest request,
            @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("抢单");

        myOrderInfoService.catchOrder(id,UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),
                UserDataUtil.getStaffHeadImg(request));
    }

//    @ApiOperation(value = "退回",notes = "处理的不行，退回重新处理")
//    @RequestMapping(value = "/my/again/{id}",method = RequestMethod.PUT)
//    public void dealAgain(HttpServletRequest request,
//                  @ApiParam(value = "remark",name = "remark") @RequestParam(value = "remark",name = "remark")  String remark,
//                  @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
//        LOG.info("退回");
//
//    }

}
